package com.kira.solo.schedule.list

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kira.solo.schedule.domain.FullCasesUseCase
import com.kira.solo.schedule.domain.ScheduleTasksService
import com.kira.solo.schedule.pojo.CasePeriodTaskInfo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 *
 * @packageName com.kira.solo.schedule.list
 * @date 2024/1/24 14:01
 * @author zhanglei
 */
@HiltViewModel
class ScheduleListViewModel @Inject constructor(
    fullCases: FullCasesUseCase,
    private val service: ScheduleTasksService,
) :
    ViewModel() {
    val cases = fullCases().combine(service.scheduledTasks) { case, info ->
        info.map { task ->
            val cases = case.cases.filter { task.caseIds.contains(it.id) }
            ScheduledCaseUiState(task, cases)
        }.run {
            ScheduledCaseListUiState(reversed())
        }
    }

    fun onDeleteTask(casePeriodTaskInfo: CasePeriodTaskInfo) {
        viewModelScope.launch {
            service.deleteTask(casePeriodTaskInfo)
        }
    }
}